<?php
/*
 * @ Nguyễn Như Khang
 * nhukhang095@gmail.com
 */
class BaseDAO{
	var $utils ;
	var $db ;
	//var $log;

	function BaseDAO()
	{
		$this->utils = new CommonUtils;
		$this->db = new DBUtils;
		///$this->log  = new Logging();
	}

	function create($table, $fields, $values)
	{
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
			
		$query = "INSERT INTO " . $table . " (";
		$queryValues = "";
		$bFirst = true;
		for($i = 0; $i < count($fields); $i++) {
			if (!$bFirst) {
				$query .= ", ";
			}
			$query .= $fields[$i];
			$bFirst = false;
		}
		$bFirst = true;
		for($i = 0; $i < count($values); $i++) {
			if (!$bFirst) {
				$queryValues .= ", ";
			}
			$queryValues .= "'" . $values[$i] . "'";
			$bFirst = false;
		}
		$query .= ") VALUES (" . $queryValues . ")";
		$res = $this->db->executeQuery($query);
		if (isset($res) ) {
			$result= mysql_query ('SELECT LAST_INSERT_ID()') ;
			if ($result) {
				$nrows = mysql_num_rows($result);
				$row = mysql_fetch_row($result);
				$lastID = $row[0];
			}
		}
		$res=$lastID;
		$this->db->closeConnection();
		return $res;
	}
	function del($table, $id, $idFldName = 'id'){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "DELETE FROM $table WHERE $idFldName = '" . $id . "'";
		$res = $this->db->executeQuery($query);
		$this->db->closeConnection();
		return $res;
	}

	function executeUpdate($table, $query){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$res = $this->db->executeQuery($query);
		$this->db->closeConnection();
		return $res;
	}

	function update($table, $fields, $values, $idFldName = 'id')
	{
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "UPDATE $table SET ";
		$bFirst = true;
		for($i = 0; $i < count($fields); $i++) {
			if (!$bFirst) {
				$query .= ", ";
			}
			$query .= $fields[$i] . " = '" . $values[$i] . "'";
			$bFirst = false;
		}
		$query .= " WHERE $idFldName = '". $values[0] . "'";

		$res = $this->db->executeQuery($query);
		$this->db->closeConnection();
		return $res;
	}
	function getItem($table, $fields, $id, $idFldName = 'id'){
		$conn = $this->db->getConnection();
		if (!$conn){
			echo 'connect fail';
			return false;
		}
		$query = "SELECT * " . " FROM " . $table . " WHERE  $idFldName = '$id'";
		$result  = $this->db->executeQuery($query);
		//echo $query;
		if ($result) {
			$row = $this->db->getReader($result) ;
			$enquiry = array();
			foreach($fields as $f) {
				if($f == "updatedby" || $f == "createdby"){
					$mRresult = $this->db->executeQuery("select fullname from user where id = '".$row[$f]."'");
					if($mRresult){
						$mRow = mysql_fetch_row($mRresult);
						if($mRow){
							$enquiry[$f] = $mRow[0];
							$enquiry[$f.'_id'] = $row[$f];
						}
					}
				}else{
					$enquiry[$f] = $row[$f];
				}
			}

		}
		$this->db->closeConnection();
		return $enquiry;
	}

	function checkDuplicate($table, $field, $value, $id=''){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$num=0;
		$query = "SELECT count(*) as num " . " FROM " . $table . " WHERE ";
		$query .= $field. "='" . $value . "'";
		if($id != ""){
			$query .= " AND id != '".$id."'";
		}
		//echo $query;
		$result=  $this->db->executeQuery($query) ;
		if ($result) {
			$row = mysql_fetch_row($result);
			$num= $row[0];
		}
		$this->db->closeConnection();
		return $num;
	}

	function getItems($table, $fields, $queryFilter = '', $orderby = '', $begin = -1, $limit = '', $formatDate = ''){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT * " . " FROM " . $table . " WHERE  1 = 1 ";
		if (isset($queryFilter) && $queryFilter != '') {
			$query .=  " AND " . $queryFilter;
		}
		if (isset($orderby) && $orderby != '') {
			$query .= " $orderby";
		}
		if (isset($begin) && $begin >= 0) {
			$query .= "  LIMIT $begin, $limit";
		}
		//echo $query;
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$returnres = array();
			while ($row = $this->db->getReader($result)) {
				$enquiry = array();
				foreach($fields as $f) {
					if($f == "updatedby" || $f == "createdby"){
						if($row[$f] != 0){
							$mRresult = $this->db->executeQuery("select fullname from user where id = '".$row[$f]."'");
							if($mRresult){
								$mRow = mysql_fetch_row($mRresult);
								if($mRow){
									$enquiry[$f] = $mRow[0];
									$enquiry[$f.'_id'] = $row[$f];
								}
							}
						}else{
							$enquiry[$f] = 0;
							$enquiry[$f.'_id'] = 0;
						}
					}else if($f == "createddate" && $formatDate != ""){
				        $y = substr($row[$f], 0, 4);
				        $m = substr($row[$f], 5, 2);
				        $d = substr($row[$f], 8, 2);
				        $s = (int) substr($row[$f], -2, 2);
				        $i = (int) substr($row[$f], -5, 2);
				        $h = (int) substr($row[$f], -7, 2);
				        $enquiry[$f] = date($formatDate, mktime($h,$i,$s,$m,$d,$y));
					}else{
						$enquiry[$f] = $row[$f];
					}
				}
				$returnres[] = $enquiry;
			}
			$this->db->closeConnection();
			return $returnres;
		}
		return false;

	}
	
	function getUserMenus($userid, $fields){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "select m.name, m.url from menu m left join user_menu um on(m.id = um.menuid)";
		$query .= " where um.userid = " . $userid . " order by m.displayorder asc";
		
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$returnres = array();
			while ($row = $this->db->getReader($result)) {
				$enquiry = array();
				foreach($fields as $f) {
					$enquiry[$f] = $row[$f];
				}
				$returnres[] = $enquiry;
			}
			$this->db->closeConnection();
			return $returnres;
		}
		return false;

	}

	function getTotalItems($table, $queryFilter){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT COUNT(*) as totalItem " . " FROM " . $table;
		if (isset($queryFilter)) {
			$query .= " WHERE " . $queryFilter;
		}
		//echo $query;
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$row = $this->db->getReader($result);
			return $row['totalItem'];
		}
		$this->db->closeConnection();
		return 0;
	}
	
	function getContents($fields, $queryFilter = '', $orderby = '', $begin = -1, $limit = ''){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT ct.id, ct.title, ct.image, ct.brief, ct.content, ct.createddate, ct.updateddate";
		$query .= ", ct.createdby, ct.updatedby, ct.status, ct.views, ct.likes";
		
		$query .= " FROM content ct WHERE 1";
		if (isset($queryFilter) && $queryFilter != '') {
			$query .=  " AND " . $queryFilter;
		}
		$query .= " group by ct.id ";
		if (isset($orderby) && $orderby != '') {
			$query .= " $orderby";
		}
		if (isset($begin) && $begin >= 0) {
			$query .= "  LIMIT $begin, $limit";
		}
		
		//echo $query;
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$returnres = array();
			while ($row = $this->db->getReader($result)) {
				$enquiry = array();
				foreach($fields as $f) {
					if($f == "updatedby" || $f == "createdby"){
						$mRresult = $this->db->executeQuery("select fullname from user where id = '".$row[$f]."'");
						if($mRresult){
							$mRow = mysql_fetch_row($mRresult);
							if($mRow){
								$enquiry[$f] = $mRow[0];
								$enquiry[$f . '_id'] = $row[$f];
							}
						}
					}else{
						$enquiry[$f] = $row[$f];
					}
				}
				$returnres[] = $enquiry;
			}
			$this->db->closeConnection();
			return $returnres;
		}
		return false;

	}
	
	function getImages($fields, $queryFilter = '', $orderby = '', $begin = -1, $limit = ''){
		$conn = $this->db->getConnection();
		if (!$conn){
			return false;
		}
		$query = "SELECT ct.id, ct.title, ct.image, ct.createddate, ct.updateddate";
		$query .= ", ct.createdby, ct.updatedby, ct.status, ct.views";
		
		$query .= " FROM image ct WHERE 1";
		if (isset($queryFilter) && $queryFilter != '') {
			$query .=  " AND " . $queryFilter;
		}
		$query .= " group by ct.id ";
		if (isset($orderby) && $orderby != '') {
			$query .= " $orderby";
		}
		if (isset($begin) && $begin >= 0) {
			$query .= "  LIMIT $begin, $limit";
		}
		
		//echo $query;
		$result  = $this->db->executeQuery($query);
		if ($result) {
			$returnres = array();
			while ($row = $this->db->getReader($result)) {
				$enquiry = array();
				foreach($fields as $f) {
					if($f == "updatedby" || $f == "createdby"){
						$mRresult = $this->db->executeQuery("select fullname from user where id = '".$row[$f]."'");
						if($mRresult){
							$mRow = mysql_fetch_row($mRresult);
							if($mRow){
								$enquiry[$f] = $mRow[0];
								$enquiry[$f . '_id'] = $row[$f];
							}
						}
					}else{
						$enquiry[$f] = $row[$f];
					}
				}
				$returnres[] = $enquiry;
			}
			$this->db->closeConnection();
			return $returnres;
		}
		return false;

	}
}
?>